# -*- encoding:utf-8 -*-
"""
@作者：ven
@文件名：visitor_order.py
@时间：2022-04-02  15:31
@文档说明:
"""
import time
from faker import Faker
from common.baseactivity import BaseActivity

f = Faker(locale='zh_CN')


class VisitorOrder(BaseActivity):

    def __init__(self):
        super().__init__()
        self.qustion = ('-android uiautomator', 'text("否")')
        self.image_upload = ('-android uiautomator', 'className("android.widget.Image")')
        self.from_photo = ('-android uiautomator', 'text("从相册选择")')
        self.choose = ('-android uiautomator', 'text("完成")')
        self.photo = ('id', 'com.tencent.mm:id/gqx')  # 进入相册后，选取相册中的图片
        self.btn_next = ('-android uiautomator', 'text("下一步")')

        self.beVisitorName = ('-android uiautomator', 'text("请添加被拜访人信息")')
        self.search_beVisitorName = ('-android uiautomator', 'text("请输入姓名")')
        self.search_btn = ('-android uiautomator', 'text("搜索")')
        self.choose_beVisitorName = ('-android uiautomator', 'text("测试1")')
        self.addVisitorName = ('-android uiautomator', 'text("添加来访人员")')

        self.visitorCompany = ('-android uiautomator', 'textContains("输入来访单位")')
        self.visitorName = ('-android uiautomator', 'textContains("请输入访客姓名")')
        self.gender = ('-android uiautomator', 'textContains("请选择性别")')
        self.phone = ('-android uiautomator', 'text("请输入手机号")')
        self.cardType = ('-android uiautomator', 'text("请选择证件类型")')
        self.card = ('-android uiautomator', 'text("身份证")')
        self.cardNum = ('-android uiautomator', 'text("请输入有效证件号码")')
        self.pic = ('-android uiautomator', 'text("照片")')
        self.ok_btn = ('-android uiautomator', 'text("确定")')
        #self.visitor_photo = ('-android uiautomator', 'text("请添加来访事由")')
        self.bth_ok = ('-android uiautomator', 'text("确认")')
        self.is_none = ('-android uiautomator', 'text("不能为空")')

        self.entourage = ['-android uiautomator', 'textContains("添加随行人员{}")']
        self.carNum = ('-android uiautomator', 'text("车牌号码")')

        self.start_time = ('-android uiautomator', 'textContains("选择开始时间")')
        self.end_time = ('-android uiautomator', 'textContains("选择结束时间")')
        self.arrive_type = ('-android uiautomator', 'text("请选择到访类型")')
        self.arrive_type_choose = ('-android uiautomator', 'text("供应商")')
        self.arrive_reason = ('-android uiautomator', 'textContains("添加来访事由")')
        self.arrive_reason_key = ('-android uiautomator', 'text("来访事由")')
        self.submit = ('-android uiautomator', 'text("提交")')

    def action(self):

        '''当前健康状况页面'''
        for _ in self.eles_locator(self.qustion):  # 当前健康状况全部选择“是”
            _.click()
        self.eles_locator(self.image_upload)[-1].click()
        self.ele_click(self.from_photo) #从相册选择
        self.ele_click(self.photo)
        self.ele_click(self.choose)   #选择后，点击“完成”
        time.sleep(2)
        self.scroll_up()
        self.ele_click(self.btn_next)
        time.sleep(2)

    def action_order(self):
        '''被拜访人信息录入'''
        self.ele_click(self.beVisitorName)
        self.ele_click(self.search_beVisitorName)
        time.sleep(1)
        self.ele_sendkeys(self.search_beVisitorName, '李鑫')
        self.ele_click(self.search_btn)
        self.ele_click(self.choose_beVisitorName)
        time.sleep(1)

        # 选择拜访开始时间
        self.ele_click(self.start_time)
        self.ele_click(self.bth_ok)

        # 选择拜访结束时间
        self.ele_click(self.end_time)
        time.sleep(2)
        self.scroll_up()
        self.ele_click(self.bth_ok)


        self.ele_click(self.arrive_type)
        self.ele_click(self.ok_btn)
        time.sleep(1)
        self._reason = f.paragraph()
        try:
            self.ele_sendkeys(self.arrive_reason, self._reason)
        except:
            if self.eles_locator(self.is_none):
                self.ele_sendkeys(self.arrive_reason, self._reason)
        self.ele_click(self.arrive_reason_key)



        '''主拜访人登记'''

        for i in range(0, 1):

            while True:
                try:
                    print(i)
                    if i == 0:
                        self.ele_click(self.addVisitorName)
                        self.action_order_main_visitor(f.name(), f.company(), f.phone_number(), f.ssn())
                    else:
                        '''随行人员登记'''
                        self.entourage[-1] = self.entourage[-1].format(i)
                        print(self.entourage)
                        self.ele_click(self.entourage)
                        cardNum = f.ssn()
                        print(f'身份证号：{cardNum}')
                        self.action_order_main_visitor(f.name(), f.company(), f.phone_number(), cardNum,
                                                       isMainVisitor=False)

                except:
                    print('未定位到元素')
                    continue
                else:
                    break

        time.sleep(2)


        self.ele_click(self.submit)

    def action_order_main_visitor(self, name, visitorCompany, phone, cardNum,isMainVisitor=True):
        '''主拜访人信息登记'''
        self.ele_sendkeys(self.visitorName, name)

        self.ele_sendkeys(self.visitorCompany, visitorCompany)
        try:
            if self.eles_locator(self.is_none):
                self.ele_sendkeys(self.visitorCompany, visitorCompany)
        except:
            pass

        self.ele_click(self.gender)
        self.ele_click(self.ok_btn)
        self.ele_sendkeys(self.phone, phone)
        self.ele_click(self.cardType)
        self.ele_click(self.ok_btn)
        self.ele_sendkeys(self.cardNum, cardNum)

        if isMainVisitor: #如果是主拜访人，上传头像
            self.ele_click(self.pic)
            self.ele_click(self.from_photo)
            self.ele_click(self.photo)
            self.ele_click(self.choose)
            time.sleep(2)
            self.scroll_up()
        self.ele_click(self.carNum)
        self.ele_click(self.bth_ok)


if __name__ == '__main__':
    visObj = VisitorOrder()
    visObj.action()
    visObj.action_order()
